import javax.microedition.lcdui.*;
// import javax.microedition.rms.RecordStore;  
// import javax.microedition.rms.RecordStoreException;
import java.util.Random;
import java.io.*;
// import java.lang.Math;
// import javax.microedition.media.*;
// import javax.microedition.media.control.*;
#include "defines.h"
public class cGame extends Canvas implements Runnable
{
	public static Graphics s_g;
	public DemoMIDlet midlet;
	int width, height;
	Image m_imgBackground = null;
	ASprite spr;
	int m_iPressedKeyCode = -1;
	int m_iRealesedKeyCode = -1;
	boolean m_IsRunning = true;
	
	public cGame()
	{
		spr = new ASprite();
		if(spr.LoadSprite("/sprite.bin"))
		{
			spr.PrintInfo();
		}
		
		new Thread(this).start();
		setFullScreenMode(true);
		// #define Read32() (int)(data[offset++]&0xFF) | ((data[offset++]&0xFF)<<8) | ((data[offset++]&0xFF)<<16) | ((data[offset++]&0xFF)<<24)
		byte[] data= new byte[4];
		
		data[0]= (byte)0xAA;
		data[1]= (byte)0xFF;
		data[2]= (byte)0x00;
		data[3]= (byte)0xBB;
		
		int offset=0;
		int k=((data[offset++]&0xFF)<<16) | ((data[offset++]&0xFF)<<8) | (data[offset++]&0xFF) | ((data[offset++]&0xFF)<<24);
		System.out.println("<<<<<<<<<<< "+k);
		

	}
	
	public int MyRandNumber(int a, int b)
	{
		if(a>b)
			return -1;
		Random rand = new Random(System.currentTimeMillis());
		int n = rand.nextInt();
		if(n<0)
			n=-n;
		return a + n % (b-a);
	}
	
	protected void hideNotify()
	{
#ifdef USE_DEBUG
		System.out.println("-----> hideNotify()");
#endif
	}
	
	protected void showNotify()
	{
#ifdef USE_DEBUG
		System.out.println("-----> showNotify()");
#endif
	}
	int i = 0;
	public void run()
	{
		while(m_IsRunning)
		{
			try{
				Thread.sleep(1000);
			}catch(Exception e){
			}
			Update();
			repaint();
			serviceRepaints();
		}
	}
	

	protected void keyPressed(int keyCode)
	{
		m_iPressedKeyCode = keyCode;
#ifdef USE_DEBUG
		System.out.println("Pressed key code: "+keyCode);
#endif
	}
	
	protected void keyReleased(int keyCode)
	{
		m_iRealesedKeyCode = keyCode;
#ifdef USE_DEBUG
		System.out.println("Released key code: "+keyCode);
#endif
	}
	void Update()
	{
		i++;
		if(i>spr.NumOfModules()-1)
		{
			i=0;
		}
		switch(m_iRealesedKeyCode)
		{
			case Keys.LSK:
				spr.Free();
				midlet.Exit();
				m_IsRunning = false;
			break;
			
			default:
			break;
		}
	}
	public void paint(Graphics g)
	{
		s_g = g;
		width = getWidth();
		height = getHeight();
		s_g.setColor(255,0,255);
		s_g.fillRect(0,0,width,height);
		// s_g.drawImage(m_imgBackground, 0, 0, 0);
		spr.PaintModule(s_g, i,30,30);
	}
}